package cn.com.tiptop.license.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import cn.com.tiptop.comon.AjaxObject;
import cn.com.tiptop.comon.Code;
import cn.com.tiptop.comon.model.AuthorizeModel;
import cn.com.tiptop.comon.model.UserModel;
import cn.com.tiptop.util.HardwareUtil;
import cn.com.tiptop.util.authorize.AuthorizeUtil;
import cn.com.tiptop.util.log.Logger;

/**
 * 
 * @author admin
 *
 */
@Controller
@RequestMapping("/")
public class IndexController {
	
	private Logger logger = Logger.getLogger(IndexController.class);
	
//	@RequestMapping("")
//	public String goToIndex(HttpServletRequest request, HttpServletResponse response) {
//		System.out.println("this is gotoindex!");
//		return "redirect:/index";
//	}
//	
//	@RequestMapping("/index")
//	public String index(HttpServletRequest request, HttpServletResponse response) {
//		System.out.println("this is index!");
//		return "index";
//	}
	
	@RequestMapping("/test")
	@ResponseBody
	public AjaxObject test(HttpServletRequest request, HttpServletResponse response) {
		System.out.println("this is test!");
		return new AjaxObject(200,"产品机器信息",HardwareUtil.getProductInformation());
	}
	
	@RequestMapping("/login")
	@ResponseBody
	public AjaxObject login(String userName,String password,HttpServletRequest request, HttpServletResponse response){
		String checkUser ="admin";
		String checKPassword ="admin";
		if(checkUser.equals(userName)&&checKPassword.equals(password)) {
			UserModel userModel = new UserModel(userName,"","配置管理员");
			request.getSession().setAttribute("ONLINE_USER",userModel);
			//return new AjaxObject(200,"登录成功",userModel);
			return new AjaxObject(Code.REQUEST_SUCCESS,Code.getMsg(Code.REQUEST_SUCCESS),userModel);
		}else {
			//return new AjaxObject(100,"登录失败,账号或密码错误");
			return new AjaxObject(Code.OFF_LINE,Code.getMsg(Code.OFF_LINE));
		}
	}
	
	@RequestMapping("/checkLogin")
	@ResponseBody
	public AjaxObject checkLogin(HttpServletRequest request, HttpServletResponse response){
		try {
			
			AuthorizeModel authorizeModel = AuthorizeUtil.checkAuthorize();
			//未授权或权限过去或者权限未生效
			if(authorizeModel.getCode()!=0) {
				return new AjaxObject(authorizeModel.getCode(),authorizeModel.getMsg(),"");
			//已授权，检查登录
			}
			
			//检查登录
			UserModel userModel = (UserModel) request.getSession().getAttribute("ONLINE_USER");
			if(userModel!=null) {
				return new AjaxObject(Code.ON_LINE,Code.getMsg(Code.ON_LINE),userModel);
			}
			//未登录
			return new AjaxObject(Code.OFF_LINE,Code.getMsg(Code.OFF_LINE));
		}catch (Exception e) {
			logger.exception(e);
			return new AjaxObject(Code.TRY_EXCEPTION,Code.getMsg(Code.TRY_EXCEPTION));
		}
	}
}
